<?php
class VotesController extends AppController {

	var $name = 'Votes';
	var $components = array('RequestHandler');

	function rate($pid,$v) {
    $this->checkSession();
		if(!$pid) exit;
		if(!is_int((int)$v) || (int)$v>5 || (int)$v<0) exit;
		$this->layout="ajax";
    $post=$this->Vote->Post->findById($pid,array('Post.id','Post.user_id','Post.voted'),null,-1);
 		if(empty($post) || !$post['Post']['voted'] || $post['Post']['user_id']==$this->luser['id']) {
    	exit;
		}
		$vote=$this->Vote->find('Vote.post_id='.$post['Post']['id'].' AND Vote.user_id='.$this->luser['id']." AND Vote.comment_id=0",null,null,-1);
		$curvote=0;
		if(empty($vote)) {
			$this->Vote->create();
      $this->data['Vote']['user_id']=$this->luser['id'];
			$this->data['Vote']['post_id']=(int)$pid;
			$this->data['Vote']['value']=$v;
		}
		else {
			$curvote=$vote['Vote']['value'];
			$this->data=$vote;
			$this->data['Vote']['value']=$v;
			$this->data['Vote']['created']=date('Y-m-d H:i:s');
		}
		$this->Vote->unbindModel(array('belongsTo' => array('Post','User','Comment')));
    if($this->Vote->save($this->data)) {
				$this->Vote->Post->rate((int)$pid,$v,$curvote);
		}
		$this->set('rating', $this->Vote->Post->find('Post.id='.(int)$pid,array('Post.id','Post.points','Post.rated'),null,-1));
    if(!$this->RequestHandler->isAjax()) {
				$this->Session->setFlash('Vote saved.');
				$this->redirect('/topic/'.$pid.'.html');
				exit;
		}
	}

	function add($cid=null,$v=null) {
		$this->checkSession();

		if(!$cid) exit;

		$this->layout="ajax";

		$comment=$this->Vote->Comment->findById($cid,null,null,-1);
		if(empty($comment) || $comment['Comment']['del'] || $comment['Comment']['user_id']==$this->luser['id']) {
			exit;
		}
		$postc=$this->Vote->Post->findById($comment['Comment']['post_id'],"Post.voted",null,-1);
		if(!$postc['Post']['voted']) {
    	exit;
		}

		$cuid=$comment['Comment']['user_id'];

		$uvotes=$this->Vote->find('Vote.comment_id='.$comment['Comment']['id'].' AND Vote.user_id='.$this->luser['id'],null,null,-1);
		if (!empty($uvotes)) { // already voted
    	exit;
		}

		if((int)$v > 0)
			$v=1;
		else
			$v=-1;

		$pid=$comment['Comment']['post_id'];

		$this->Vote->create();
		$this->data['Vote']['user_id']=$this->luser['id'];
		$this->data['Vote']['post_id']=(int)$pid;
		$this->data['Vote']['comment_id']=(int)$cid;
		$this->data['Vote']['value']=$v;

		$this->Vote->unbindModel(array('belongsTo' => array('Post','User','Comment')));
    if($this->Vote->save($this->data)) {
				// change comment vote
				$this->Vote->Comment->inc((int)$cid,'vote',$v);
				// change user vote
				$this->Vote->User->inc($cuid,'vote',$v);
		}
        // return comment's vote
		$this->set('vote', $this->Vote->Comment->find('Comment.id='.(int)$cid,array('Comment.vote'),null,-1));
    if(!$this->RequestHandler->isAjax()) {
				$this->Session->setFlash('Vote saved.');
				$this->redirect('/topic/'.$pid.'.html');
				exit;
		}
	}

	function admin_index() {
		$this->checkAdmin(); $this->layout='admin';
		$this->Vote->recursive = 0;
    $count=$this->Vote->findCount();
		$this->set('count',$count);
		$this->set('votes', $this->Vote->findAll(null,null,"Vote.created DESC",APAGESIZE,$this->pager($count,APAGESIZE),0));
	}

	function admin_delete($id = null) {
		$this->checkAdmin(); $this->layout='admin';
		if(!$id) {
			$this->redirect('/admin/votes/');
			exit;
		}
		if($this->Vote->del($id)) {
			$this->Session->setFlash('Vote deleted: id '.$id.'');
			$this->redirect('/admin/votes/');
		}
	}

}
?>